import Vue from 'vue'
import Vuex from 'vuex'
import { ajax } from './../api/index'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()], // 持久化 Vuex 变成 localStorage 存储在本地
  state: {
    count: 1000,
    cartNum: 16,
    city: null, // 城市信息
    cityList: [],
    userInfo: null,
    cinemaList: [],
    goodList: []
  },
  mutations: {
    changeCount(state, payload) {
      state.count += payload
    },
    changeCity(state, payload) {
      state.city = payload
    },
    changeCityList(state, payload) {
      state.cityList = payload
    },
    changeUserInfo(state, payload){
      state.userInfo = payload
    },
    getCinemaList(state, payload){
      state.cinemaList = payload
    },
    getGoodList(state, payload){
      state.goodList = payload
    }
  },
  actions: {
    changeCountAction({ commit }, payload) {
      commit('changeCount', payload)
    },
    async changeCityList({ commit }, payload){
      let res = await ajax.getMaiData(...payload)
      commit('changeCityList', res.data.cities)
    },
    async getCinemaList({ commit }, payload){
      let  res = await ajax.getMaiData(...payload)
      commit('getCinemaList', res.data.cinemas)
    },
    async getGoodListAction({ commit }, payload){
      let res = await ajax.getGoodList(payload)
      console.log(res);
      commit('getGoodList', res)
    },
  },
  getters: {
    hotCity(state) {
      return state.cityList.filter(item => item.isHot == 1)
    },
    typeList(state){
      var obj = {}
      var arr = []
      state.goodList.forEach((item) => {
        if(!obj[item.type.value]){
          // obj[item.type.value]  = obj.shirt 
          obj[item.type.value] = item.type.text  // obj.shirt = 衬衫 
          arr.push(item.type)
        }   
      })
      console.log(arr)
      return arr
    }
  },
  modules: {
  }
})
